import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
const fs = require('fs')
const path = require('path')
const fsExtra = require('fs-extra')
const childProcess = require('child_process')
import { getFilesAll, getFileList, getJsonFiles } from './utils/copyFolder'
import { openZip } from './utils/zip'

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    contextBridge.exposeInMainWorld('fs', fs)
    contextBridge.exposeInMainWorld('path', path)
    contextBridge.exposeInMainWorld('fsExtra', fsExtra)
    contextBridge.exposeInMainWorld('getFilesAll', getFilesAll)
    contextBridge.exposeInMainWorld('getFileList', getFileList)
    contextBridge.exposeInMainWorld('getJsonFiles', getJsonFiles)
    contextBridge.exposeInMainWorld('openZip', openZip)
    contextBridge.exposeInMainWorld('childProcess', childProcess)
    contextBridge.exposeInMainWorld('selectFolder', {
      selectFolder: (options) => {
        return ipcRenderer.invoke('select-folder', options)
      }
    })
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
